import $service from '../../../../service'
const app = getApp();
Page({
  data: {
    inquiryData: {},
    inquiryLogHealthGuidanceList: [], //健康指导
    formListResult: [], // 申请单列表
    inspectionOpinionList: [], //检查单列表
    prescriptionList: [], //处方列表
    hospitalizationDetailResp: null, // 住院建议书
    id: '',
    imgList: []
  },
  onLoad(e) {
    this.setData({
      id: e.id
    })
    this.getAllInfo()
  },
  getAllInfo() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    app.http.getAllInfo({id: this.data.id}).then(res => {
      let imgList = []
      res.data.prescriptionList.forEach(item => {imgList.push(item.imgArr)})
      this.setData({
        inquiryData: res.data,
        inquiryLogHealthGuidanceList: res.data.inquiryLogHealthGuidanceList,
        formListResult: res.data.formListResult,
        inspectionOpinionList: res.data.inspectionOpinionList,
        prescriptionList: res.data.prescriptionList,
        imgList,
        hospitalizationDetailResp: res.data.hospitalizationDetailResp,
      })
    }).catch(err => {
      $service.showToast(err.msg)
    }).finally((() => {
      wx.hideLoading({})
    }))
  },
  // 查看详情
  viewDetail(e) {
    let {id, template, type} = e.currentTarget.dataset
    $service.openPage(`/pages/user_side/index/index_health_guidance/index_health_guidance?id=${id}&type=${type}&value=${template}`)
  },
  // 保存处方
  save(e) {
    let {src} = e.currentTarget.dataset
    wx.showLoading({
      title: '图片保存中',
    })
    wx.downloadFile({
      url: src,
      success: function (res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权")
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  } else {
                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          },
          complete(res){
            wx.hideLoading({})
          }
        })
      }
    })
  },
  // 购买处方
  buy(e) {
    let {id, no, state, store} = e.currentTarget.dataset
    if (state == 1) {
      app.http.getIsPrescriptionOrder({prescriptionId: id}).then(res => {
        if (res.data.orderSn) {
          $service.openPage(`/pages/prescription_flow/order_msg/order_msg?prescription_id=${store == 0 ? id : res.data.orderSn}&type=${store == 0? 1: 0}`)
        } else {
          $service.openPage(`/pages/prescription_flow/order_create/order_create?prescription_id=${id}&prescriptionNo=${no}&ishospitalPre=${store}`)
        }
      })
    } 
    else if(state == 2){
      $service.showToast('您的处方审核审核不通过')
    }
    else {
      $service.showToast('您的处方正在审核中，请等待审核通过后再购买')
    }
  },
  // 图片放大
  previewImage(e) {
    let {src} = e.currentTarget.dataset;
    wx.previewImage({
      current: src,
      urls: [src.replace(/https/, 'http')]
    })
  },
  //查看患者详情
  goToDetailed(){
    $service.openPage(`/pages/user_side/my_doctor/detailedMessage/detailedMessage?id=${this.data.id}`)
  }
})